var express = require('express');
var router = express.Router();

var InhouseController = require('../controller/InhouseController.js');
var OuthouseController = require('../controller/OuthouseController.js');
var TotalController = require('../controller/TotalController.js');
var inhouseController = new InhouseController();
var outhouseController = new OuthouseController();
var totalController = new TotalController();

var inhouseData = new Array(); //预处理后的入库数据
var outhouseData = new Array(); //预处理后的出库数据
var dealedData = new Array();

//获取数据
router.get('/getInhouseData', function(req, res, next) {
  	inhouseController.getInhouseOrderAll(function(data){
 		inhouseData = data;
 		if(inhouseData !== [] && outhouseData !== []){
		 	res.send(inhouseData);
		} else {
		 	res.send('load data error!');
		}
 	})  
});

router.get('/getOuthouseData', function(req, res, next) {
 	outhouseController.getOuthouseOrderAll(function(value){
	 	outhouseData = value;
	 	if(outhouseData !== []){
		 	res.send(inhouseData);
		} else {
		 	res.send('load data error!');
		}
	});  
});
 
//处理数据
router.get('/computeData', function(req, res, next) {
	totalController.computeDataByNum(inhouseData,outhouseData,function(data){
		dealedData = data;
		if(dealedData !== []){
	 		res.send(dealedData);
	 	} else {
	 		res.send('deal data error!');
	 	}
	})
});

//导入数据
router.get('/importData',function(req, res, next) {
	inhouseController.saveDealData(dealedData[0],function(value){
		res.send(value);
	});
	outhouseController.saveDealData(dealedData[1],function(value){
		res.send(value);
	});
})

module.exports = router;
